package gretha.panels;

import gretha.Jtables.JTableMassif;
import gretha.Jtables.utils.boutonSupprimerCellEditor;
import gretha.Jtables.utils.boutonSupprimerCellRenderer;
import gretha.donnees.CohorteDuMassif;
import gretha.donnees.Massif;
import gretha.modelsDeJTable.MassifTableModel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

public class PanelListeMassifs extends JPanel {
	private static final long serialVersionUID = 1L;
	
	public static CreationScenario parent;
	
	public static JComboBox listeMassifs;
	
	JButton boutonAjouterMassif;
	
	private PanelListeMassifs panelScenario;
	
	private JPanel hautDePanel;
	
	private JPanel corps;
	
	private JScrollPane scrollPanMassifs;

	private static int[][] idDesMassifs;

	private String[] nomMassifs;
	
	private JTableMassif tblm;
	

	public PanelListeMassifs(CreationScenario panel, ArrayList<Massif> massifs) {
		parent = panel;

		setBorder(new EmptyBorder(10, 10, 10, 10));
		
		GridBagLayout gbl = new GridBagLayout();
	    this.setLayout(gbl);
	    
	    // Add the display to the top four cells
	    GridBagConstraints contraintesHautDePanel = new GridBagConstraints();
	    contraintesHautDePanel.gridx = 0;
	    contraintesHautDePanel.gridy = 0;
	    contraintesHautDePanel.gridwidth = 1;
	    contraintesHautDePanel.gridheight = 1;
	    contraintesHautDePanel.fill = GridBagConstraints.BOTH;
	    
	    // Add the display to the top four cells
	    GridBagConstraints contraintesCorps = new GridBagConstraints();
	    contraintesCorps.gridx = 0;
	    contraintesCorps.gridy = 1;
	    contraintesCorps.gridwidth = 1;
	    contraintesCorps.gridheight = 2;
	    contraintesCorps.fill = GridBagConstraints.BOTH;
		
		corps = new JPanel();
		hautDePanel = new JPanel();
		hautDePanel.setSize(new Dimension(100,200));
		hautDePanel.setBackground(Color.white);
		corps.setLayout(new BorderLayout());
		corps.setSize(new Dimension(100,200));
		corps.setBackground(Color.white);
		
		// Données
		idDesMassifs = new int[25][25];
		nomMassifs = new String[25];
		recupererDonnees(massifs);
		
		// Ajout des données
		listeMassifs = new JComboBox(nomMassifs);
		listeMassifs.setBackground(Color.white);

		hautDePanel.add(new JLabel("Liste des massifs"));
		hautDePanel.add(listeMassifs);

		// Ajout du bouon ajouterMassif
		boutonAjouterMassif = new JButton("Ajouter");
		panelScenario = this;

		// Ajout de l'actionListener
		java.awt.event.ActionListener ajouterMassif = new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				corps.removeAll();
				Massif massifTemporaire = new Massif();
				massifTemporaire
						.recupererDonnees(getIdDesMassifs()[listeMassifs
								.getSelectedIndex()][0]);

				parent.getListeDesMassifsDuScenario().add(massifTemporaire);
				parent.setListeMassifsTableau(new Massif[parent.getListeDesMassifsDuScenario().size()]);

				/*
				 * On récupère les données du massif
				 */
				CohorteDuMassif[] donnees = new CohorteDuMassif[massifTemporaire
						.getCohortes().size()];
				int i = 0;
				for (CohorteDuMassif cm : massifTemporaire.getCohortes()) {
					donnees[i] = cm;
					i++;
				}
				
				

				int k = 0;
				for (Massif m : parent.getListeDesMassifsDuScenario()) {
					parent.getListeMassifsTableau()[k] = m;
					k++;
				}
				
				tblm = new JTableMassif(new MassifTableModel(
						parent.getListeMassifsTableau(),
						parent.getListeDesMassifsDuScenario()));
				tblm.setDefaultRenderer(JButton.class,
						new boutonSupprimerCellRenderer());
				tblm.setDefaultEditor(JButton.class,
						new boutonSupprimerCellEditor(tblm, panelScenario));
				tblm.setMaximumSize(new Dimension(300,100));
				tblm.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
				
				scrollPanMassifs = new JScrollPane(tblm);
				scrollPanMassifs.setPreferredSize(tblm.getMaximumSize());
				corps.add(scrollPanMassifs);

				/*
				 * Recuperation des cohortes et insertion au pied
				 */
				corps.revalidate();
				corps.repaint();
				parent.rafraichir();
			}
		};
		boutonAjouterMassif.addActionListener(ajouterMassif);
		hautDePanel.add(boutonAjouterMassif);

		scrollPanMassifs = new JScrollPane();
		corps.add(scrollPanMassifs);
		
	    gbl.setConstraints(hautDePanel, contraintesHautDePanel);
	    gbl.setConstraints(corps, contraintesCorps);
		
		add(hautDePanel);
		add(corps);
		
	}
	
	private void recupererDonnees(ArrayList<Massif> massifs) {
		int i = 0;
		for (Massif m : massifs) {
			nomMassifs[i] = m.getNom();
			idDesMassifs[i][0] = m.getId();
			i++;
		}
	}
	
	public void rafraichir(){
		hautDePanel.revalidate();
		hautDePanel.repaint();
		tblm.revalidate();
		tblm.repaint();
		scrollPanMassifs.revalidate();
		scrollPanMassifs.repaint();
		corps.revalidate();
		corps.repaint();
	}
	
	public void setListeDesMassifsDuScenario(Massif[] donnees) {
		parent.setListeDesMassifsDuScenario(donnees);
	}


	public static int[][] getIdDesMassifs() {
		return idDesMassifs;
	}

	public void setIdDesMassifs(int[][] idDesMassifs) {
		PanelListeMassifs.idDesMassifs = idDesMassifs;
	}

	public String[] getNomMassifs() {
		return nomMassifs;
	}

	public void setNomMassifs(String[] nomMassifs) {
		this.nomMassifs = nomMassifs;
	}

	public static JComboBox getListeMassifs() {
		return listeMassifs;
	}

	public void setListeMassifs(JComboBox listeMassifs) {
		PanelListeMassifs.listeMassifs = listeMassifs;
	}



}
